package controller.base;

import domain.entity.base.MaintainBaseInfo;
import domain.service.base.MaintainBaseService;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by bowen on 2017-08-28.
 * 大型设备数据
 */
@Controller
public class MaintainBaseController {

    private static final Logger logger = LoggerFactory.getLogger(MaintainBaseController.class);

    @Autowired
    private MaintainBaseService baseService;

    @RequestMapping(value = "createMaintainBase", method = RequestMethod.POST)
    @ResponseBody
    public String create(@RequestParam("facility") String facility, @RequestParam("materiel") String materiel) {
        List<MaintainBaseInfo> baseInfos = new ArrayList<MaintainBaseInfo>();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
        try {
            List<MaintainBaseInfo> facilitys = objectMapper.readValue(facility, new TypeReference<List<MaintainBaseInfo>>() {
            });
            if (facilitys != null) {
                baseInfos.addAll(facilitys);
            }
            List<MaintainBaseInfo> materiels = objectMapper.readValue(materiel, new TypeReference<List<MaintainBaseInfo>>() {
            });
            if (materiels != null) {
                baseInfos.addAll(materiels);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        baseService.create(baseInfos);
        return "保存成功!";
    }


    @RequestMapping(value = "getBaseFacilitys")
    @ResponseBody
    public Map<String, List<MaintainBaseInfo>> getBaseFacilitys() {
        return baseService.getBaseFacilitys();
    }

}
